package com.ecarx.camera.worker.camera.dvr.impl;

import static com.ecarx.camera.constant.Constants.DEFAULT_LOOP_RECORD_DURATION;

import com.ecarx.camera.data.ThumbnailData;
import com.ecarx.camera.utils.LogUtils;
import com.ecarx.camera.worker.camera.dvr.IDvrAPI;

import java.util.ArrayList;
import java.util.List;

/**
 * Dvr的Debug实现（仅用于模拟调试）
 */
public class DvrDebugAPIImpl implements IDvrAPI {

    private static final String TAG = "DvrApiDebugImpl";

    private final List<IDvrStateWatcher> mDvrStateWatchers = new ArrayList<>();
    private final List<ISdcardStateWatcher> mSdcardStateWatchers = new ArrayList<>();
    private final List<IOperateResultsWatcher> mOperateResultWatchers = new ArrayList<>();

    private boolean mIsDvrGeneralRecording = false;
    private boolean mIsDvrEmergencyRecording = false;

    /**
     * 判断是否已经注册过Dvr状态监听
     */
    private boolean mIsDvrStateObserverRegistered = false;

    @Override
    public void init() {
        registerDvrStateObserver();
    }

    @Override
    public void destroy() {
        unregisterDvrStateObserver();
    }

    @Override
    public boolean registerDvrStateWatcher(IDvrStateWatcher watcher) {
        return watcher != null && !mDvrStateWatchers.contains(watcher) && mDvrStateWatchers.add(watcher);
    }

    @Override
    public void unregisterDvrStateWatcher(IDvrStateWatcher watcher) {
        if (watcher != null) {
            mDvrStateWatchers.remove(watcher);
        }
    }

    @Override
    public boolean registerSdcardStateWatcher(ISdcardStateWatcher watcher) {
        return watcher != null && !mSdcardStateWatchers.contains(watcher) && mSdcardStateWatchers.add(watcher);
    }

    @Override
    public void unregisterSdcardStateWatcher(ISdcardStateWatcher watcher) {
        if (watcher != null) {
            mSdcardStateWatchers.remove(watcher);
        }
    }

    @Override
    public boolean registerOperateResultsWatcher(IOperateResultsWatcher watcher) {
        return watcher != null && !mOperateResultWatchers.contains(watcher) && mOperateResultWatchers.add(watcher);
    }

    @Override
    public void unregisterOperateResultsWatcher(IOperateResultsWatcher watcher) {
        if (watcher != null) {
            mOperateResultWatchers.remove(watcher);
        }
    }

    @Override
    public boolean openDvr() {
        return true;
    }

    @Override
    public boolean exitDvr() {
        return true;
    }

    @Override
    public boolean startGeneralRecord() {
        LogUtils.d(TAG, "startGeneralRecord");
        for (IOperateResultsWatcher item : mOperateResultWatchers) {
            item.onDvrGeneralRecordSuccess();
        }
        mIsDvrGeneralRecording = true;
        return true;
    }

    @Override
    public boolean startEmergencyRecord() {
        LogUtils.d(TAG, "startEmergencyRecord");
        for (IDvrStateWatcher item : mDvrStateWatchers) {
            item.onDvrStateEmergencyRecording();
        }
        mIsDvrEmergencyRecording = true;
        return true;
    }

    @Override
    public boolean pauseGeneralRecord() {
        LogUtils.d(TAG, "pauseGeneralRecord");
        for (IOperateResultsWatcher item : mOperateResultWatchers) {
            item.onDvrPauseGeneralRecordSuccess();
        }
        mIsDvrGeneralRecording = false;
        return true;
    }

    @Override
    public boolean doCapture() {
        LogUtils.d(TAG, "doCapture");
        for (IOperateResultsWatcher item : mOperateResultWatchers) {
            item.onDvrCaptureSuccess();
        }
        return true;
    }

    @Override
    public boolean doDvrFactoryReset() {
        LogUtils.d(TAG, "doDvrFactoryReset");
        return true;
    }

    @Override
    public boolean doDvrSdcardFormat() {
        LogUtils.d(TAG, "doDvrSdcardFormat");
        return true;
    }

    @Override
    public boolean wakeupEthernet() {
        LogUtils.d(TAG, "wakeupEthernet");
        return true;
    }

    @Override
    public boolean isDvrSdcardExists() {
        return true;
    }

    @Override
    public boolean isDvrGeneralRecording() {
        return mIsDvrGeneralRecording;
    }

    @Override
    public boolean isDvrEmergencyRecording() {
        return mIsDvrEmergencyRecording;
    }

    private void registerDvrStateObserver() {
        if (mIsDvrStateObserverRegistered) {
            LogUtils.w(TAG, "registerDvrStateObserver, has already registered.");
            return;
        }
        mIsDvrStateObserverRegistered = true;
    }

    private void unregisterDvrStateObserver() {
        if (mIsDvrStateObserverRegistered) {
            LogUtils.d(TAG, "unregisterDvrStateObserver");
            mIsDvrStateObserverRegistered = false;
        }
    }

    /**
     * Dvr缩略图的Debug实现
     */
    public static class DvrThumbnailAPIDebugImpl implements IDvrThumbnailAPI {

        @Override
        public ThumbnailData getDvrLatestThumbnail() {
            return null;
        }
    }

    /**
     * Dvr配置的Debug实现
      */
    public static class DvrConfigAPIDebugImpl implements IDvrConfigAPI {

        @Override
        public boolean isDvrDeviceSupported() {
            return true;
        }

        @Override
        public int getDvrGeneralRecordResolution() {
            return 480;
        }

        @Override
        public int getDvrGeneralRecordDuration() {
            return DEFAULT_LOOP_RECORD_DURATION;
        }

        @Override
        public boolean getDvrGeneralRecordMicMute() {
            return false;
        }

        @Override
        public boolean setDvrGeneralRecordResolution(int resolution) {
            return true;
        }

        @Override
        public boolean setDvrGeneralRecordDuration(int duration) {
            return true;
        }

        @Override
        public boolean setDvrGeneralRecordMicMute(boolean mute) {
            return true;
        }

        @Override
        public String getDvrSoftWareVersion() {
            return "debug_1.0";
        }

        @Override
        public String getDvrHardWareVersion() {
            return "debug_h_1.0";
        }

        @Override
        public int getSdcardUsedPercentage() {
            return 0;
        }

        @Override
        public boolean registerDvrConfigWatcher(IDvrConfigWatcher watcher) {
            return true;
        }

        @Override
        public void unregisterDvrConfigWatcher(IDvrConfigWatcher watcher) {
        }
    }

}
